package com.leyou.cart.controller;

import com.leyou.cart.pojo.Cart;
import com.leyou.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author: Saka
 * Date: 2019/2/25
 * Time: 10:39
 */
@RestController
public class CartController {


    @Autowired
    private CartService cartService;

    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart) {

        this.cartService.addCart(cart);

        return ResponseEntity.ok().build();

    }

    @DeleteMapping("{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId") Long skuId) {
        this.cartService.deleteCart(skuId);
        return ResponseEntity.ok().build();


    }

    @GetMapping
    public ResponseEntity<List<Cart>> queryCart() {
        List<Cart> carts = this.cartService.queryCart();
        if (carts != null && 0 != carts.size()) {
            return ResponseEntity.ok(carts);

        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    @PutMapping
    public ResponseEntity<Void> updateNum(@RequestParam("skuId") Long skuId,
                                          @RequestParam("num") Integer num) {

        this.cartService.updateNum(skuId, num);
        return ResponseEntity.ok().build();

    }

    @PostMapping("addCarts")
    public ResponseEntity<Void> addCarts(@RequestBody List<Cart> carts) {

        this.cartService.addCarts(carts);

        return ResponseEntity.ok().build();

    }
}
